<template>
  <div class="app-container" v-if="show">
    <el-form
      class="ry_form"
      v-show="showSearch"
      :inline="true"
      label-width="100px"
      :model="queryParams"
      ref="queryForm"
      size="medium"
    >
      <el-form-item :label="$t('common.type')" prop="panelType">
        <DictRadio
          v-model="queryParams.panelType"
          :radioData="dict.type.wms_inventory_panel_type"
          size="small"
          @change="handleQuery"
        ></DictRadio>
      </el-form-item>
      <el-form-item :label="$t('common.status')" prop="status">
        <DictRadio
          v-model="queryParams.status"
          :radioData="statusList"
          size="small"
          @change="handleQuery"
        ></DictRadio>
      </el-form-item>
      <el-form-item label="仓库" prop="warehouseId">
        <!-- <wms-warehouse-cascader
          v-model="queryParams.place"
        ></wms-warehouse-cascader> -->
        <WmsAreaCascader
          v-model="queryParams.warehouseId"
          size="small"
        ></WmsAreaCascader>
      </el-form-item>
      <el-form-item label="SKU" prop="skuCode">
        <el-input v-model="queryParams.skuCode" :placeholder="$t('common.search')"></el-input>
      </el-form-item>
      <el-form-item label="箱号" prop="boxCode">
        <el-input v-model="queryParams.boxCode" :placeholder="$t('common.search')"></el-input>
      </el-form-item>
      <el-form-item label="单号查询" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入单号"></el-input>
      </el-form-item>
      <!-- <el-form-item :label="$t('common.inventory')" prop="quantity">
        <number-range v-model="queryParams.quantityRange"></number-range>
      </el-form-item> -->
      <el-form-item v-if="YT_roles" :label="$t('common.customer')" prop="customerId">
        <WmsCustomerSelect v-model="queryParams.customerId"></WmsCustomerSelect>
      </el-form-item>
      <el-form-item label="ASN号" prop="asn">
        <el-input v-model="queryParams.asn" :placeholder="$t('common.search')"></el-input>
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="handleQuery"
          >{{ $t('common.search') }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >{{ $t('common.reset') }}</el-button
        >
      </el-form-item>
    </el-form>
    <el-row class="mb8" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wms:item:export']"
          >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="unpackingInTheWarehouse"
          >库内拆箱
        </el-button>
      </el-col>
      <right-toolbar
        :columns="columns"
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <BoxListComponent
      ref="boxList"
      :url="'/wms/omsPurchaseBox/list'"
      :methodType="'post'"
      :return-data="returnBoxData"
      :params="{ ...queryParams, ...boxForm }"
      :pageReq="pageReq"
      :bigBoxSvg="'max-width: 170%;'"
      :checkbox="checkbox"
      :isBoxList="true"
      :styleBox="'height: calc(100vh - 210px);overflow: auto;'"
      @selection-change="handleSelectionBoxChange"
    />
  </div>
</template>

<script>
import NP from "number-precision";
import { listWmsInventory, exportWmsInventory, unpackBox } from "@/api/wms/inventory";
import NumberRange from "@/components/NumberRange";
import ItemSelect from "@/components/ItemSelect";
import PanelByItem from "@/views/wms/inventory/component/PanelByItem.vue";
import PanelByItemType from "@/views/wms/inventory/component/PanelByItemType.vue";
import PanelByWarehouse from "@/views/wms/inventory/component/PanelByWarehouse.vue";
import { mapGetters } from "vuex";

// import BoxListComponent from "@/views/components/wms/BoxList/index.vue";
import BoxListComponent from './BoxList/index.vue'
import WmsAreaCascader from "@/components/WmsAreaCascader";

export default {
  name: "WmsInventory",
  components: {
    PanelByWarehouse,
    PanelByItemType,
    PanelByItem,
    ItemSelect,
    NumberRange,
    BoxListComponent,
    WmsAreaCascader,
  },
  props: {
    checkbox: {
      type: Boolean,
      default: true,
    }
  },
  dicts: ["wms_inventory_panel_type"],
  data() {
    return {
      show: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      wmsInventoryList: [],
      // 查询参数
      queryParams: {
        warehouseId: null,
        status: null,
        orderNo: null,
        boxCode: null,
        skuCode: null,
        pageSize: 10,
        asn: null
      },
      pageReq: {
        page: 1,
        size: 10,
      },
      // 表单参数
      form: {},
      columns: [],
      panelType: 5,
      chooseList: [],
      statusList: [
        {label: '待出库', value: 'TO_OUTBOUND'},
        {label: '已出库', value: 'OUT_BOUND'},
        {label: '已入库', value: 'IN_BOUND'},
        // {label: '已拆箱', value: 'UNPACK'},
      ]
    };
  },
  async created() {
    this.show = true || res;
    if (this.show) {
      this.getList();
    }
  },
  computed: {
    ...mapGetters(["userId"]),
    currentComponent() {
      let type = parseInt(this.panelType);
      switch (type) {
        case 5:
          return "panel-by-warehouse";
        case 10:
          return "panel-by-area";
        case 20:
          return "panel-by-item-type";
        default:
          return "";
      }
    },
    boxForm() {
      if (this.queryParams.warehouseId) {
        return {
          warehouseId: this.queryParams.warehouseId[0],
          areaId: this.queryParams.warehouseId[1],
        };
      }
    },
    YT_roles() {
      // 暂时判断roles为common不是客户的未YT
      if(this.$store.state.user.roles[0] !== 'common') {
        return true
      }
    }
  },
  methods: {
    getList() {
      this.$nextTick(() => {
        this.$refs.boxList.getList();
      });
    },
    returnBoxData(data) {
      if (data.content) {
        return data.content;
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemId: null,
        warehouseId: null,
        quantity: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    unpackingInTheWarehouse() {
      if(this.chooseList.length === 0) {
        this.$message.warning('请至少选择一个箱子!')
      } else {
        const boxIds = this.chooseList.map(r=>r.id)
        unpackBox({boxIds:boxIds}).then(res=>{
          if(res.includes('不在')) {
            this.$message.error(res)
            this.chooseList = []
            this.getList()
          } else {
            this.$message.success('操作成功!')
          }
        })
      }
    },
    handleSelectionBoxChange(v) {
      this.chooseList = v
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.pageReq.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal
        .confirm("是否确认导出所有库存数据项？")
        .then(() => {
          this.exportLoading = true;
          return exportWmsInventory(queryParams);
        })
        .then((response) => {
          this.$download.download(response);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
